const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail: {},
    money:'',
    withdrawok:false,
    withdrawInfo:{},
    selectIndex:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    })
    wx.setNavigationBarTitle({
      title: '提现',
    })
    wx.hideShareMenu({

    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.fetchData();

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  fetchData: function () {
    wx.request({
      url: app.globalData.api + '/users/index.html&token=' + wx.getStorageSync('token'),
      method: 'get',
      success: (res) => {
        if (res.data.code == 0) {
          wx.hideLoading();
          this.setData({
            detail: res.data.data
          })
        }
        else {
          this.fetchError(res.data.code);
        }
      }
    })
  },
  fetchError: function (code) {
    if (code == 40001) {
      wx.navigateTo({
        url: '/pages/auth/getuserinfo',
      })
    }

  },
  moneyinput:function(e){
    if (this.data.detail.money < e.detail.value){
      this.setData({
        money: this.data.detail.money
      })
    }
    else{
      this.setData({
        money: e.detail.value
      })
    }


  },
  selectMoney:function(e){
    console.log(e.currentTarget.dataset.money);
    if (this.data.detail.money < e.currentTarget.dataset.money) {
      this.setData({
        money:'',
        selectIndex: '',
      })
      wx.showToast({
        title: '余额不足',
        icon:'none',
      })
    }
    else {
      this.setData({
        money: e.currentTarget.dataset.money,
        selectIndex:e.currentTarget.dataset.index,
      })
    }

  },
  copyCard:function(){
    wx.setClipboardData({
      data: this.data.withdrawInfo.customer_card,
      success: function (res) {
        wx.showToast({
          title: '已复制',
          icon:'none',
        })
      }
    })
  },
  withdraw:function(){
    if (this.data.money>=5){
    wx.request({
      url: app.globalData.api + '/withdraw/index.html&token=' + wx.getStorageSync('token'),
      method: 'post',
      data:{
        "amount": this.data.money
      },
      success: (res) => {
        if (res.data.code == 0) {
          this.setData({
            withdrawok:true,
            withdrawInfo:res.data.data,
          })
         debugger;
        }
        else {
          this.fetchError(res.data.code);
        }
      }
    })
    }
    else{
      wx.showToast({
        title: '请选择提现金额',
        icon:'none',
      })
    }

  },
  withdrawAll:function(){
    if(this.data.detail.money>=5){
      this.setData({
        money: this.data.detail.money
      })
    }
    else{
      wx.showToast({
        title: '余额满5元才能提现',
        icon:'none'
      })
    }

  },
  go_withdrawlist:function(){
    wx.navigateTo({
      url: '/pages/user/withdrawlist',
    })
  },
  continue_fn:function(){
    wx.navigateTo({
      url: '/pages/game/play',
    })
  }
})